package com.xxx.store.controller;

import com.xxx.store.entity.Address;
import com.xxx.store.service.AddressService;
import com.xxx.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addresses")
public class AddressController extends BaseController{
    @Autowired
    private AddressService addressService;

    @RequestMapping("addNewAddress")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        addressService.addNewAddress(getUidFromSession(session),getUsernameFromSession(session),address);
        return new JsonResult<>(OK);
    }
    @RequestMapping({"/",""})
    public JsonResult<List<Address>> getAddress(HttpSession session){
        List<Address> list = addressService.getAddressByUid(getUidFromSession(session));
        return new JsonResult<>(OK,list);
    }
    @RequestMapping("/setDefault/{aid}")
    public JsonResult<Void> setDefault(HttpSession session,@PathVariable("aid") Integer aid){
        addressService.setDefault(aid,getUidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }
    @RequestMapping("/deleteAddress/{aid}")
    public JsonResult<Void> deleteAddress(HttpSession session,@PathVariable("aid") Integer aid){
        addressService.delete(aid,getUidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }
}
